// disarmmine.txt - This is an unusual disarmable mine. When activated,
// it detonates a certain number of mines nearby.

// It triggers
// when party member or character friendly to the party gets within 1 space. 
// It activates 1 turn later, unless the player uses the object. In this case, 
// it can be disarmed if the Tool Use skill in the party is high enough.

// When the mine activates, it broadcasts message 100 to all nearby terrain 
// scripts. If one of those terrain scripts is chainmine, that mine will 
// explode.

// Memory Cells - 
//   0 - The radius of the detonation message it broadcasts.
//   1 - Special item for bypass. If left at 0, no special item helps 
//     bypass the trap. Otherwise, if that party has this special item, 
//     the mine will not discharge.
//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. 
//     Otherwise, this stuff done flag is set to 1 when the mine is discharged.
//     If the flag is non-zero, then the mine is disarmed and will not go off.
//     Also, if the flag is non-zero when the party enters the zone, the mine 
//     switches to its "exploded" graphic.
//   5 - Mine difficulty. The Tool Use skill necessary to disarm it. If left at
//     0, uses default value of 5.

beginterrainscript; 

variables;
	short i_am_active = 1;
	short mine_severity = 4;
	short choice,r1;
	short trip_time;
	short mine_activated = 0;
	short mine_difficulty = 5;
	
body;

beginstate INIT_STATE;
	set_script_mode(2);
	
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_flag(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_active = 0;
			flip_terrain(my_loc_x(),my_loc_y());
			}
		}

	if (get_memory_cell(0) != 0)
		mine_severity = get_memory_cell(0);
	if (get_memory_cell(5) != 0)
		mine_difficulty = get_memory_cell(5);
	set_mechanism_difficulty(mine_difficulty);
	break;

beginstate START_STATE;
	if ((mine_activated) && (i_am_active)) {
		if (tick_difference(trip_time,get_current_tick()) > 1) { // BOOM!
			broadcast_terrain_message(get_memory_cell(0),100); 
				
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
				if (get_flag(get_memory_cell(2),get_memory_cell(3)) > 0) {
					set_flag(get_memory_cell(2),get_memory_cell(3),1); 
					}
				}
			flip_terrain(my_loc_x(),my_loc_y());
			i_am_active = 0;
			mine_activated = 0;
			}
		}
		else {
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
				if (get_flag(get_memory_cell(2),get_memory_cell(3)) > 0) {
					i_am_active = 0;
					}
				}
		
			if (get_memory_cell(1) > 0)
				if (has_special_item(get_memory_cell(1)) > 0)
					end();
			if (i_am_active == 0)
				end();
				
			if (get_nearest_good_char(1) >= 0) {
				print_str_color("You have triggered a mine! It could blow any moment.",2);
				mine_activated = 1;
				trip_time = get_current_tick();
				put_sparkles_on_space(my_loc_x(),my_loc_y(),3,6);
				run_animation_sound(170);
				}
			}
break;

beginstate SEARCH_STATE;
	if (i_am_active == 0) {
		print_str_color("The mine has been deactivated. You can't do anything with it.",2);
		end();
		}
	
	if (get_highest_skill(15) < get_mechanism_difficulty()) {
		print_str_color("The mine is too complicated. You can't figure it out.",2);
		end();
		}

	play_sound(9);
	print_str_color("You disarm the mine.",2);
	i_am_active = 0;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
	flip_terrain(my_loc_x(),my_loc_y());
	award_party_xp(BASE_TRAP_XP / 2,1 + 2 * get_mechanism_difficulty());
break;
